// pages/cancel_tickets/index.js
import {navTo,navToTab,onInput,onClick,showText} from '../../utils/reuse'
import{cancelTickets,cancelTicketsSubmit,cancelTicketsList} from '../../api/index'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    page: 1,
    pagesize:10,
    list:[],
    obj:false,
    status:'数据加载完毕！',
    request_show:true,
    order_id:0,
    remark:''
  },

  onChange(event) {
    if(event.detail.name == 1){
      this.setData({
        page:1,
        list:[],
        request_show:true
      })
      this.getList()
    }
    // this.setData({
    //   active: event.detail.name,
    //   List:[],
    //   page:1,
    //   request_show:true,
    // })
  },
  getList(){
    var obj = {
      uid:wx.getStorageSync('uid'),
      shop_id:wx.getStorageSync('shop_id'),
      page:this.data.page,
      pagesize:this.data.pagesize
    }
    cancelTicketsList(obj).then(res=>{
      if(res.msg.length <= 0){
        this.setData({
          status:'数据加载完毕！',
          request_show:false
        })
      }
      if(res.sta == 1){
        if(res.msg.length < this.data.pagesize){
          this.setData({
            status:'数据加载完毕！',
            request_show:false
          })
        }

        this.setData({
          list:[...this.data.list,...res.msg]
        })
      }
    })
  },

  qrFn(){
    var that = this
    wx.scanCode({
      success(res){
        var obj = JSON.parse(res.result)
        if(new Date().getTime() - obj.current_time < 3600000){
          if(obj.order_id){
            var obj = {
              uid:wx.getStorageSync('uid'),
              order_id:obj.order_id
            }
            cancelTickets(obj).then(res=>{
              if(res.sta == 1){
                res.json = JSON.stringify(res.json)
                that.setData({
                  obj:res.msg
                })
              }
            })
          }
          // if(obj.status){
          //   wx.navigateTo({
          //     url: '/pages/course_detail_qd/index?id=' + obj.status,
          //   })
          // }
        }else{
          showText('二维码已过期！',2)
        }
      },
      fail(res){
        showText('扫码失败！',2)
      }
    })
  },
  submit(){
    var obj = {
      uid:wx.getStorageSync('uid'),
      order_id:this.data.obj.id,
      remark:this.data.remark
    }
    cancelTicketsSubmit(obj).then(res=>{
      if(res.sta == 1){
        showText(res.msg,1)
        this.setData({
          obj:false
        })
      }else{
        showText(res.msg,2)
      }
    })
  },

  nav(e){
    navTo(e)
  },
  click(e){
    onClick(e,this)
  },
  input(e){
    onInput(e,this)
  },
  cancal(){
    Dialog.confirm({
      title: '确定要取消核销吗？'
    })
    .then((res) => {
      this.setData({
        obj:false
      })
    })
    .catch((res) => {
      console.log(res)
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page:1,
      list:[],
      request_show:true
    })
    this.getList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page:this.data.page + 1,
    })
    if(this.data.request_show){
      this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})